package com.cxyzy.customview

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.view_location.view.*


/**
 * 自定义view之组合view，通过xml属性进行设置
 * 用于显示当前位置
 */
class LocationViewWithAttrs(
    context: Context,
    attrs: AttributeSet?
) : LinearLayout(context, attrs) {
    var name: String? = null
    var address: String? = null

    init {
        initTypeValue(context, attrs)
        initView(context)
    }

    private fun initTypeValue(
        context: Context,
        attrs: AttributeSet?
    ) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.LocationViewWithAttrs)
        name = typedArray.getString(R.styleable.LocationViewWithAttrs_locationName)
        address = typedArray.getString(R.styleable.LocationViewWithAttrs_locationDesc)
        typedArray.recycle()
    }

    private fun initView(context: Context) {
        LayoutInflater.from(context).inflate(R.layout.view_location, this, true)
        setData(name,address)
    }

    fun setData(name: String?, address: String?) {
        name?.let { locationNameTv.text = it }
        address?.let { locationAddressTv.text = it }
    }
}